﻿Imports System.IO
Imports System.Data.SqlClient
Imports System.Threading
Public Class Frm_Login
    Dim ligne As String = ""
    Dim bol As Boolean = False
    Public isHidden As Boolean
    Dim position As Integer = 0
    Dim thrd As Thread
    Private Sub Frm_Login_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Application.Exit()
    End Sub
    Private Sub Frm_Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Partager.flag = True
            If File.Exists(Path.GetTempPath & "SupImg.txt") = True Then
                Partager.sr = New StreamReader(Path.GetTempPath & "SupImg.txt")
                Do Until Partager.sr.Peek = -1
                    Dim ligne As String = Partager.sr.ReadLine()
                    If String.IsNullOrEmpty(ligne) = False Then
                        If File.Exists(Path.GetTempPath & ligne) Then ' tarik
                            My.Computer.FileSystem.DeleteFile(Path.GetTempPath & ligne)
                        End If
                    End If
                Loop
                Partager.sr.Close()

                If File.Exists(Path.GetTempPath & "SupImg.txt") Then ' tarik
                    File.Delete(Path.GetTempPath & "SupImg.txt")
                End If
            End If


            If File.Exists("complétion.txt") = True Then
                Partager.sr = New StreamReader("complétion.txt")
                Do Until Partager.sr.Peek = -1
                    ligne = Partager.sr.ReadLine()
                    If String.IsNullOrEmpty(ligne) = False Then
                        txtlogin.AutoCompleteCustomSource.Add(ligne)
                    End If
                Loop
                Partager.sr.Close()
            End If

            If Not Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe") Then
                Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "PieceJointe") ' C'est le dossier qui va contenir les pièces jointes
            End If
        Catch ex As Exception
            If Not ex.Message.Contains(ligne) Then
                MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            End If
        End Try
    End Sub

    Public Sub ecrireSurFichier(ByVal identif As String)
        If Not Partager.sr Is Nothing Then
            Partager.sr.Close()
            Partager.sw = New StreamWriter("complétion.txt", True)
            Partager.sw.WriteLine(identif)
            Partager.sw.Close()
        End If
    End Sub
    Private Sub Btn_Entrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Entrer.Click
        Try
            Partager.conne()
            Partager.con = New SqlConnection("Data Source=" & Partager.serveur & ";Integrated security =True;" & "Initial Catalog=Gestion_Cabinet_Avovat")
            Partager.conPhone = New SqlConnection("Data Source=" & Partager.serveur & ";Integrated security =True;" & "Initial Catalog=Gestion_Cabinet_Avovat")
            Partager.Administrat = New Administrateur
            '////////////////Administrateur
            If Txt_pass.Text = Partager.Administrat.pass And txtlogin.Text = Partager.Administrat.login Then
                Partager.moi = New Compte ' tarik
                Partager.moi.type = True ' tarik
                Partager.vider(Me)
                Partager.remplirRapportControle() 'Remplie le DataSet des sessions d'assises
                tmrCinqMin.Start() 'après 5 minute il va afficher l'alarme 
                tmrTroisHeur.Start() ' après 3 heures il va afficher l'alarme

                'Pour les rendez vous
                Partager.remplirRendezVous()
                timSec.Start()
                '////////

                Frm_Main.Show()
                isHidden = True
                Me.Hide()
                Exit Sub
            Else
                Partager.Administrat.login = ""
                Partager.Administrat.pass = ""
            End If

            '//////////////////////////Compte

            If txtlogin.Text <> "" And Txt_pass.Text <> "" Then
                Partager.Administrat.remplirListCompte()
                Partager.moi = New Compte
                For Each cmp As Compte In Partager.Administrat.listCompte
                    If cmp.user = txtlogin.Text And cmp.passe = Txt_pass.Text Then
                        Partager.moi = cmp
                        Dim isEmpty As Boolean = False
                        If File.Exists("complétion.txt") = True Then
                            Partager.sr = New StreamReader("complétion.txt")

                            Do Until Partager.sr.Peek = -1
                                ligne = Partager.sr.ReadLine()
                                If String.IsNullOrEmpty(ligne) = False Then
                                    isEmpty = True
                                    If ligne <> cmp.user Then
                                        ecrireSurFichier(cmp.user)
                                    End If
                                End If
                            Loop
                        End If
                        If Not isEmpty Then
                            ecrireSurFichier(cmp.user)
                        End If
                        
                        bol = True
                        Partager.remplirRapportControle() 'Remplie le DataSet des sessions d'assises
                        tmrCinqMin.Start() 'après 5 minute il va afficher l'alarme 
                        tmrTroisHeur.Start() ' après 3 heures il va afficher l'alarme

                        'Pour les rendez vous
                        Partager.remplirRendezVous()
                        timSec.Start()
                        '////////

                        Partager.vider(Me)
                        Frm_Main.Show()
                        isHidden = True
                        Me.Hide()
                        Exit For
                    End If
                Next

                If bol = False Then
                    MsgBox("اسم المستخدم أو كلمة المرور خاطئة", MsgBoxStyle.Critical, "خطأ")
                End If
            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
                If Not ex.Message.Contains(ligne) Then
                    MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
                End If
        End Try
    End Sub
    Private Sub Frm_Login_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        Try
            If Not isHidden Then
                Partager.moi = Nothing
                Partager.Administrat = Nothing
                bol = False
                txtlogin.Select()
            End If
        Catch ex As Exception
            If Not ex.Message.Contains(ligne) Then
                MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            End If
        End Try
    End Sub

    Private Sub tmrCinqMin_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrCinqMin.Tick
        Frm_Tanbih.Show()
        tmrCinqMin.Stop()
    End Sub

    Private Sub tmrTroisHeur_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTroisHeur.Tick
        Try
            Dim i As Integer = 0
            Dim numDossier As String = ""
            For Each ligne As DataRow In Partager.ds.Tables(0).Rows
                If numDossier <> CStr(ligne("Num_dossier")) Then
                    numDossier = CStr(ligne("Num_dossier"))
                    i += 1
                End If
            Next
            If i <> 0 Then
                Frm_Tanbih.Show()
            End If
        Catch ex As Exception
            If Not ex.Message.Contains(ligne) Then
                MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            End If
        End Try
    End Sub

    Private Sub timSec_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timSec.Tick
        Try
            Partager.thrd2 = New Thread(AddressOf forTimer)
        Catch ex As Exception
            If Not ex.Message.Contains(ligne) Then
                MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
            End If
        End Try
        Partager.thrd2.Start()
    End Sub

    Public Sub forTimer()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf forTimer))
        Else
            While Partager.isRendezVousAdd
                Dim heure As Integer

                If Not Partager.moi Is Nothing Then
                    Dim position As Int16 = 1
                    If Partager.moi.lstRendezVousStruct.Count > 0 Then
                        For Each item In Partager.moi.lstRendezVousStruct
                            heure = ((item.dateAlarme - Now).Hours * 60) + ((item.dateAlarme - Now).Minutes)
                            If heure <= 30 And heure > 0 Then
                                Partager.rendVsStruct = New Partager.rendezVousStruct
                                Partager.rendVsStruct = item
                                Partager.isRendezVousAdd = False
                                frmRendezAlerte.Show()
                                Partager.moi.lstRendezVousStruct.Remove(item)
                                Exit For 'parceque la collection été modifié
                            ElseIf position = Partager.moi.lstRendezVousStruct.Count Then
                                If timSec.Enabled Then
                                    timSec.Stop()
                                    'MsgBox("timer stopé")
                                    Partager.isRendezVousAdd = False
                                End If
                            End If
                            position += 1
                        Next
                    Else
                        timSec.Stop()
                        Partager.isRendezVousAdd = False
                    End If
                Else
                    Exit Sub
                End If
            End While
        End If
    End Sub
End Class
